import axios from 'axios';
import  { generateCookie } from '../../lib/cookie.js'

const FAKE_HEADERS = {
    Accept: "*/*",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
    Origin: "https://chat.deepseek.com",
    Pragma: "no-cache",
    Priority: "u=1, i",
    Referer: "https://chat.deepseek.com/",
    "Sec-Ch-Ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
    "Sec-Ch-Ua-Mobile": "?0",
    "Sec-Ch-Ua-Platform": '"Windows"',
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
    "X-App-Version": "20241129.1"
};

export const tokenVali = async (key = 0, value) => {
    if(key != 'DEEQSEEKTOKEN') return 
    try {
        let url = 'https://chat.deepseek.com/api/v0/users/current'
        let headers = {
            ...FAKE_HEADERS,
            Authorization: `Bearer ${value}`,
            Cookie: await generateCookie()
        };

        let resopen =  await axios.get(url, { headers: headers })
        let res = resopen.data;
        if (res.code == 0) {
            logger.info('token有效');
           return { code: 0, msg: 'token有效'  }
        } else {
            logger.error('token无效', res.msg);
            return { code: 1, msg: res.msg }
        }
    } catch (error) {
        logger.error('tokenVali error:', error);
        return;
    }
};